/**
 * @author caojj
 * @version	1.0.0.3
 * @require ["FWK.UI.Window", "FWK.UI.PageMask"]
 * @usage				
 * 			var btns = [new XN.MsgBox.Btn("OK", function() {alert("OK")}), new XN.MsgBox.Btn("No", function() {alert("No");return true;}) ]
 *			var mMsgBox = new XN.MsgBox();
 *			mMsgBox.show("标题","确定<span style='color:red'>吗？</span>", btns, true);
 */

try {
	if(!FWK.UI){
		throw "ns";
	}
} catch (ex) {
	try{FWK}catch(e){FWK={}}
	FWK.UI = {}
}

FWK.UI.MsgBox = function(id, opacity) {
	var me = this;
	
	if (!opacity) {opacity=50;}
	this.id = this.id ? this.id : "MsgBox_"+Math.random();
	this.window = new FWK.UI.Window("win_"+this.id, "");
	this.pageMask = new FWK.UI.PageMask("maks_"+this.id, this.window.getDom(), opacity);
	
	this.contentDom = document.createElement("div");
	this.btnsDom = document.createElement("p");this.btnsDom.hide = function() {me.pageMask.hide();}
	
	this.window.getDom("body").appendChild(this.contentDom);
	this.window.getDom("body").appendChild(this.btnsDom);
//	this.window.getDom("body").appendChild(document.createTextNode(" "));
	
	if (opacity!=null && opacity!=undefined) {
		this.pageMask.aimOpacity = parseInt(opacity);
	}
}

FWK.UI.MsgBox.prototype = {
	show : function(title, content, btns, closeAble, width, opacity) {
		this.window.setCloseFun(closeAble ? this.btnsDom.hide : null);
		
		if (width) {
			width = parseInt(width);
			this.window.getDom("win").style.width = width + "px";
		} else {
			this.window.getDom("win").style.width = "";
		}
		
		this.window.setTitle(title, true);
		if (this.pageMask.V.mask.style.display=="") {
			this.pageMask.open();
		} else {
			this.pageMask.show();			
		}

		
		this.btnsDom.innerHTML = "";
		if (btns&&btns.length) {
			for (var i in btns) {
				if (btns[i].dom) {
					this.btnsDom.appendChild(btns[i].dom);
				}
			}	
		} else {
			this.btnsDom.appendChild(document.createTextNode(" "));			//解决圆角问题
		}
		
		if (content) {
			this.contentDom.innerHTML = content;
		}
		
		if (opacity!=null && opacity!=undefined) {
			this.pageMask.aimOpacity = parseInt(opacity);
		}
	},
	hide : function() {
		if (this.pageMask.V.mask.style.display == "") {
			this.pageMask.hide();
		} else {
			this.pageMask.close();
		}
	}
}

FWK.UI.MsgBox.Btn = function(text, funRef, className) {
	this.dom = document.createElement("input");this.dom.type = "button";this.dom.value = text;this.dom.className=className;
	this.dom.onclick = function() {
		var ret = undefined;
		try {
			ret = funRef();
		} catch(ex) {}
		
		if (!ret) {
			this.parentNode.hide();
		}
	}
//	return this;
}
//btns - [
//	{
//		dom : null,
//		text : "按钮",
//		fun : function() {}
//	}
//]
